<?php

namespace App\Http\Resources;

use App\Models\Mint;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\File;

class NftDetailResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $content = File::get(app_path('nft/attribute_count.json'));
        // 解析JSON
        $data = json_decode($content, true);
        if($this->minted_days >= $this->days){
            $status = 'out';
        }else{
            $status = 'working';
        }

        return [
            'nft_id' => $this->id,
            'name' => $this->name,
            'symbol' => $this->symbol,
            'description' => $this->description,
            'img' => getenv('APP_URL') . '/storage' . $this->img,
            'power' => $this->power,
            'background' => $this->background,
            'background_percent' => $this->background_percent,
            'background_count' => $data['backgrounds'][$this->background],
            'core' => $this->core,
            'core_percent' => $this->core_percent,
            'core_count' => $data['cores'][$this->core],
            'gear' => $this->gear,
            'gear_percent' => $this->gear_percent,
            'gear_count' => $data['gears'][$this->gear],
            'hat' => $this->hat,
            'hat_percent' => $this->hat_percent,
            'hat_count' => $data['hats'][$this->hat],
            'traits' => $this->traits,
            'traits_percent' => $this->traits_percent,
            'traits_count' => $data['traits'][$this->traits],
            'earring' => $this->earring,
            'earring_percent' => $this->earring_percent,
            'earring_count' => $data['earrings'][$this->earring],
            'percent' => $this->percent,
            'minted_amount' => Mint::where('nft_id', $this->nft_id)->sum('amount'),
            'minted_days' => $this->minted_days,
            'days' => $this->days,
            'status' => $status,
        ];
    }
}
